from django.contrib.auth.models import Permission, ContentType
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.permission_ser import PermissionSerializer, ContentTypeSerializer
from meiduo_admin.utils import PageNum


class PermissionView(ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    # 获取所有权限
    def content_types(self, request):
        content_types = ContentType.objects.all()
        ser = ContentTypeSerializer(content_types, many=True)
        return Response(ser.data)
